# -*- coding: utf-8 -*-
from TESS_PythonAPI.Tessng import NetItemType, GraphicsItemPropName, Online
from TESS_PythonAPI.Tessng import PyCustomerNet
from TESS_PythonAPI.Tessng import tessngIFace
from net_file.config import SPEED_LIMIT_CONFIG,DEF_SPEED_LIMIT_CONFIG


# 用户插件子类，代表用户自定义与路网相关的实现逻辑，继承自MyCustomerNet
class MyNet(PyCustomerNet):
    def __init__(self, config: dict):
        super(MyNet, self).__init__()
        self.config = config

    # 过载的父类方法，当打开网后TESS NG调用此方法
    #     实现的逻辑是：路网加载后获取路段数，如果路网数为0则调用方法createNet构建路网，之后再次获取路段数，如果大于0则启动仿真
    def afterLoadNet(self):
        # 代表TESS NG的接口
        iface = tessngIFace()
        # 代表TESS NG的路网子接口
        netiface = iface.netInterface()
        net = netiface.netAttrs()
        # 获取路段数
        count = netiface.linkCount()
        netiface.buildNetGrid(4)
        # simuiface = iface.simuInterface()
        # simuiface.setSimuAccuracy(10)
        # simuiface.setAcceMultiples(3)
        # speed = self.config['speed']
        links = netiface.links()

        for link in links:
            link.setLimitSpeed(DEF_SPEED_LIMIT_CONFIG)
            for SPEED_LIMIT in SPEED_LIMIT_CONFIG:
                if link.id() in SPEED_LIMIT['link_list']:
                    link.setLimitSpeed(SPEED_LIMIT['max_speed'])
                    break

        # for link in links:
        #     sec_from_connect_link = link.fromConnectors()
        #     if len(sec_from_connect_link) == 0:
        #         dp = netiface.createDispatchPoint(link)
            # sec_to_connect_link = link.toConnectors()
            # if len(sec_to_connect_link) == 0:
            # dp = netiface.createDispatchPoint(link)
        # print("道路限速完成")
        # vehiType_proportion_lst = []
        # # 车型组成：小客车 0.3，大客车 0.2，公交车 0.1，货车 0.4
        # vehiType_proportion_lst.append(Online.VehiComposition(1, 0.3))
        # vehiType_proportion_lst.append(Online.VehiComposition(2, 0.2))
        # vehiType_proportion_lst.append(Online.VehiComposition(3, 0.1))
        # vehiType_proportion_lst.append(Online.VehiComposition(4, 0.4))
        # vehiCompositionID = netiface.createVehicleComposition(" 动 态 创 建 车 型 组 成 ", vehiType_proportion_lst)
        # if link1:
        #     dp = netiface.createDispatchPoint(link1)
        #     if dp:
        #         dp.addDispatchInterval(vehiCompositionID, 600, 300)

    # 是否允许用户对路网元素的绘制进行干预，如选择路段标签类型、确定绘制颜色等，本方法目的在于减少不必要的对python方法调用频次
    def isPermitForCustDraw(self):
        # 代表TESS NG的接口
        iface = tessngIFace()
        netface = iface.netInterface()
        count = netface.linkCount()

        print(count)
        netFileName = netface.netFilePath()
        if "Temp" in netFileName:
            return True
        else:
            return False

    ''' 过载的父类方法，在绘制路网元素时被调用，确定用ID或名称，以及字体大小绘制标签，也可确定不绘制标签
    itemType：NetItemType常量，代表不同类型路网元素
    itemId：路网元素的ID
    ref_outPropName：返回值，GraphicsItemPropName枚举类型，影响路段和连接段的标签是否被绘制
        GraphicsItemPropName。None_表示不绘制，GraphicsItemPropName。ID：表示绘制ID，GraphicsItemPropName。NAME:表示绘制名称
    ref_outFontSize，返回值，标签大小，单位：米。假设车道宽度是3.5米，如果ref_outFontSize.value等于7，绘制的标签大小占两个车道宽度
    '''

    def ref_labelNameAndFont(self, itemType, itemId, ref_outPropName, ref_outFontSize):
        # 代表TESS NG的接口
        iface = tessngIFace()
        # 代表TESS NG仿真子接口
        simuiface = iface.simuInterface()
        # 如果仿真正在进行，设置ref_outPropName.value等于GraphicsItemPropName.None_，路段和车道都不绘制标签
        if simuiface.isRunning():
            ref_outPropName.value = GraphicsItemPropName.None_
            return
        # 默认绘制ID
        ref_outPropName.value = GraphicsItemPropName.Id
        # 标签大小为6米
        ref_outFontSize.value = 6
        # 如果是连接段一律绘制名称
        if itemType == NetItemType.GConnectorType:
            ref_outPropName.value = GraphicsItemPropName.Name
        elif itemType == NetItemType.GLinkType:
            if itemId == 1 or itemId == 5 or itemId == 6:
                ref_outPropName.value = GraphicsItemPropName.Name

    # 过载父类方法，是否绘制车道中心线
    def isDrawLaneCenterLine(self, laneId):
        return True

    # 过载父类方法，是否绘制路段中心线
    def isDrawLinkCenterLine(self, linkId):
        if linkId == 1:
            return False
        else:
            return True
